import { _decorator, Button, Component, EditBox, game, Label, Node, Sprite } from 'cc';
import { HttpCom } from '../net/HttpCom';
import { MyButton } from '../commonComp/MyButton';
import { GameEvent } from '../EventName';
const { ccclass, property } = _decorator;

@ccclass('Activity')
export class Activity extends Component {

    @property(Node)
    pages: Node = null;
    @property(Node)
    top: Node = null;
    

    @property(EditBox)
    cdkEditBox: EditBox = null;

    @property(Button)
    cdkBtn: Button = null;


    start() {
        game.on(GameEvent.SHOW_STORE_PAGE,this.SHOW_STORE_PAGE,this)
        this.cdkBtn.interactable = false;
        this.cdkBtn.node.getComponent(Sprite).grayscale = true;
        this.init();
    }

    SHOW_STORE_PAGE(){
        this.initTop(1);
        this.pages.children[1].active = true;
        this.pages.children[2].active = false;
        this.pages.children[0].active = false;
    }

    init(){
        this.pages.children[1].active = false;
        this.pages.children[2].active = false;
        this.pages.children[0].active = true;

        this.initTop(0);
    }

    initTop(index: number){
        this.top.children.forEach((e,i)=>{
            e.children[0].children[0].active = false;
            e.children[0].children[1].active = false;
        })

        this.top.children[index].children[0].children[0].active = true;
        this.top.children[index].children[0].children[1].active = true;
    }

    cdkVerify(){
        let cdk = this.cdkEditBox.string;
        if(cdk == ""){
            game.emit(GameEvent.SHOW_TOAST,"please enter cdk")
            return;
        }
        HttpCom.cdkVerify(cdk);
    }

    onCdkEditBoxChange(){
        if(this.cdkEditBox.string == ""){
            this.cdkBtn.interactable = false;
            this.cdkBtn.node.getComponent(Sprite).grayscale = true;
        }else{
            this.cdkBtn.interactable = true;
            this.cdkBtn.node.getComponent(Sprite).grayscale = false;
        }
    }


    onTaskBtnClick(){
        this.changePage(0);
        this.initTop(0);
    }

    onStoreBtnClick(){
        this.changePage(1);
        this.initTop(1);
    }

    onInviteBtnClick(){
        this.changePage(2);
        this.initTop(2);
    }

    changePage(index: number){
        this.pages.children.forEach((e,i)=>{
            e.active = false;
        })

        this.pages.children[index].active = true;
    }

    update(deltaTime: number) {
        
    }
}


